﻿using System;
using System.IO;
using System.Linq;
using System.Net.Mail;
using TaskKernel.Configuration;
using TaskKernel.Domain.Services.Action;
using TaskKernel.Infrastructure;

namespace TaskKernel.Domain.Services.Mail
{
    public class MessageService : IMessageService
    {
        private readonly IMessageSender _messageSender;
        private readonly ITaskAction _taskAction;

        public MessageService(IMessageSender messageSender, ITaskAction taskAction)
        {
            _messageSender = messageSender;
            _taskAction = taskAction;
        }

        #region IMessageService Members

        public bool Sended { get; private set; }

        public void ChangeNotification(Task task, Account account, string action)
        {
            if (!task.GetUncommittedChanges().Any()) return;
            Message = new TaskMessage(new BodyTemplateChange(task, action))
            {
                MessageSubject = task.TaskName,
                MessageSenderDisplayName = task.Department.DepartmentName,
                MessageSender = !String.IsNullOrEmpty(task.Department.MailBox)
                                    ? task.Department.MailBox
                                    : TaskKernelConfiguration.SmtpSender
            };
            var taskRecipient = (task.Department.IsClaim || task.Department.IsClaimNotify)
                                    ? new DepartmentClaimMailrecipient(Message.RecipientsAddress)
                                    : new DepartmentMailRecipient(Message.RecipientsAddress);
            taskRecipient.AddParticipants(task);
            taskRecipient.AddOwner(task);
            taskRecipient.AddDepartmentUsers(task);
            taskRecipient.AddAdmins(task);
            Message.RecipientsAddress.Remove(account.Mail);
            Sended = _messageSender.SendMessage(Message);
        }

        public TaskMessage Message { get; private set; }

        public void ReminderNotification(Reminder reminder)
        {
            Message = new TaskMessage(new BodyTemplateReminder(reminder))
            {
                MessageSubject = reminder.Task.TaskName + " [Напоминание]",
                MessageSenderDisplayName = "Напоминание",
                MessageSender = TaskKernelConfiguration.SmtpSender
            };
            Message.RecipientsAddress.Add(reminder.Owner.Mail);
            Sended = _messageSender.SendMessage(Message);
        }

        public void RememberCloseNotification(Task task)
        {
            Message = new TaskMessage(new BodyTemplateRememberClose(task))
            {
                MessageSubject = task.TaskName + " [Необходимо закрыть задачу]",
                MessageSenderDisplayName = "Напоминание",
                MessageSender = TaskKernelConfiguration.SmtpSender
            };
            Message.RecipientsAddress.Add(task.Owner.Mail);
            Sended = _messageSender.SendMessage(Message);
        }

        public void ContentAlexandrovaNotification(Task task, Account account)
        {
            Message = new TaskMessage(new BodyTemplateContentAlexandrova(task))
            {
                MessageSubject = task.TaskName,
                MessageSenderDisplayName = task.Department.DepartmentName,
                MessageSender = account.Mail
            };
            Message.RecipientsAddress.Add(account.Mail);
            var files = _taskAction.LoadFiles(task).ToList();
            foreach (var attachment in files){
                Message.Attachments.Add(new Attachment(new MemoryStream(attachment.Content), attachment.FileName, "text/plain"));
            }
            Sended = _messageSender.SendMessage(Message);
        }

        #endregion
    }
}